//
//  DHNewFeatureController.swift
//  Weibo
//
//  Created by Holfioo on 15/9/5.
//  Copyright © 2015年 Holfioo. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class DHNewFeatureController: UICollectionViewController {

    init () {
        // 流水布局
        super.init(collectionViewLayout: UICollectionViewFlowLayout())
    }
    // 用于xib
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView?.backgroundColor = UIColor.whiteColor()
        // 注意修改此处的关系。手写代码
        self.collectionView!.registerClass(DHNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        setFlowlayout()
    }
    
    private func setFlowlayout() {
        
        let flow = collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
        // 设置流水布局的样式
        flow.minimumInteritemSpacing = 0
        flow.minimumLineSpacing = 0
        flow.itemSize = view.bounds.size
        flow.scrollDirection = UICollectionViewScrollDirection.Horizontal // 水平滚动
        // 设置collection的样式
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
    }

    // MARK: UICollectionViewDataSource

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return 4
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        // 只需要 讲起视为 自定义的即可
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! DHNewFeatureCell
        // 数值传入cell 中
        cell.imageIndex = indexPath.item
        
        return cell
    }
    
    // 代理方法中实现按钮的出现，动画效果
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        // 实现动画效果
        let index = collectionView.indexPathsForVisibleItems().last! //设置当前是最后一个课件的
        if index.item == 4 - 1 {
            let cell = collectionView.cellForItemAtIndexPath(index) as! DHNewFeatureCell
            cell.showStartBtn()
        }
    }
    
}

// 对于较少的属性设置，可以直接在此进行另外一个类

private class DHNewFeatureCell : UICollectionViewCell {
    // 重写获得方法
    private var imageIndex: NSInteger = 0 {
        didSet {
            imaView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            startBtn.hidden = true
            
        }
    }
    func showStartBtn() {
        startBtn.hidden = false
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        // 带弹簧效果的动画
        UIView.animateWithDuration(1.5, delay: 0.0, usingSpringWithDamping: 1, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            //
            self.startBtn.transform = CGAffineTransformIdentity // 恢复到默认的状态
            }){ (_) -> Void in
                
        }
    }
    
    // 监听按钮的点击
    @objc func clickStartBtn() {
//        printLog(__FUNCTION__)
        // 发送通知，因为改变跟控制器的方法，并不是NewFeatuer的方法
//        NSNotificationCenter.defaultCenter().postNotificationName(DHSwitchRootViewController, object: nil)
        NSNotificationCenter.defaultCenter().postNotificationName(DHSwitchRootViewController, object: nil)
    }
    
    // 布局页面属性
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
    
    private func setupUI() {
        addSubview(imaView)
        addSubview(startBtn)
        
        imaView.frame = bounds // 等于cell自身的bounds
        // 改变其属性
        startBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -150))
    }
    
    //: 懒加载控件
    private lazy var imaView = UIImageView()
    private lazy var startBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("开始体验", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.sizeToFit()
        // 监听方法
        btn.addTarget(self, action: "clickStartBtn", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
}
